package com.luyuan.crm.member.web.provider;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.Customer;
import com.luyuan.crm.member.service.CustomerService;

@Component("memberMoblieProvider")
public class MemberMoblieProvider {
    @Resource(name = "customerService")
    private CustomerService customerService;

    public List<Customer> getSuggestionItems(Object arg) {
        String s = String.valueOf(arg) + "%";
        Criterion criterion = Restrictions.and(
                Restrictions.or(Restrictions.like("mobile", s), Restrictions.like("name", s)),
                Restrictions.sqlRestriction("discriminator = 'member'"));

        List<Customer> members = customerService.findBy(criterion, 10);
        List<Customer> result = new ArrayList<Customer>(10);
        for (Customer member : members) {
            Customer m = new Customer();
            m.setId(member.getId());
            m.setName(member.getName());
            m.setMobile(member.getMobile());
            result.add(m);
        }
        return result;
    }
}
